package luaj.utils;

import java.io.File;

import luaj.Globals;
import luaj.LoadState;
import luaj.LuaValue;
import luaj.compiler.LuaC;
import luaj.lib.TwoArgFunction;
import luaj.lib.jse.CoerceJavaToLua;
import luaj.lib.jse.JavaPackage;
import luaj.utils.functions.*;

public class Script extends TwoArgFunction {
    private static Globals globals;
    private static File path;
    public Script(Globals globals, File path) {
        this.globals = globals;
        this.path = path;
        init();
    }

    @Override
    public LuaValue call(LuaValue mode, LuaValue env) {
        env.set("runThread", new runThread());
        env.set("startActivity", new startActivity(globals, path));
        env.set("http", CoerceJavaToLua.coerce(HttpLib.class));
        env.set("Ticker", CoerceJavaToLua.coerce(Ticker.class));
        env.set("android", new JavaPackage("android"));
        env.set("java", new JavaPackage("java"));
        env.set("org", new JavaPackage("org"));
        return env;
    }

    private void init() {
        this.globals.load(this);
        this.globals.load(new JsonLib());
        LoadState.install(this.globals);
        LuaC.install(this.globals);
    }
}
